package com.choudou5.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.choudou5.bo.UDeskBo;
import com.choudou5.bo.UDeskIvrBo;
import com.choudou5.utils.ConfigUtil;
import com.choudou5.utils.LogUtil;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UDeskService {


	public Map findOrderInfo(UDeskBo bo){
		if(bo == null)
			return buildReturn(false, "参数为空");
		if(!validSign(bo.getTimestamp(), bo.getSign()))
			return buildReturn(false, "sign验证失败");
		UDeskIvrBo ivrBo = bo.getData();
		if(ivrBo == null)
			return buildReturn(false, "data参数为空");
		if(StrUtil.isBlank(ivrBo.getParam1()))
			return buildReturn(false, "param1参数为空，无法查询订单");
		String message = ivrBo.getUdesk_from_number()+" 输入 "+ivrBo.getParam1() +" 查询成功, 请查收短信";
        return buildReturn(true, message);
	}

	private boolean validSign(int timestamp, String sign){
		String str = ConfigUtil.get("udesk.token") + timestamp;
		LogUtil.debug("validSign_", str);
		String code = SecureUtil.md5(str);
		return sign.equals(code);
	}

	private Map buildReturn(boolean success, String message){
		Map result = new HashMap(1);
		Map data = new HashMap();
		data.put("code", success?1:0);
		data.put("message", message);
		result.put("data", data);
		return result;
	}

}
